$(document).ready(function () {
    //验证登录
    $.ajax({
        type: "GET",
        url: "load",
        dataType: "json",
        data: {},
        timeout: 10000,
        async:false,
        cache:false,
        success: function (res) {
            if(res.result != "yes") {
                window.location.href = "login";
            }
        }
    });
    //注销绑定方法
    $("#logout").click(function () {
        $.ajax({
            type: "POST",
            url: "logout",
            dataType: "json",
            data: {},
            timeout: 10000,
            async:false,
            success: function (res) {
                if (res.result != 'SUCCESS') {
                    alert(res.reason);
                }else
                    window.location.href = "login.html";


            }
        });
    });
    //获取url参数
    var uurl = location.search;
    var gid = uurl.split('&')[0].split('=')[1];
    var pid = uurl.split('&')[1].split('=')[1];

    //填充页面信息
    $.ajax({
        type: "GET",
        url: "index/good/info",
        dataType: "json",
        data: {
            "id":gid
        },
        timeout: 10000,
        async:false,
        success: function (res){
            if(res.result != "SUCCESS"){
                alert(res.reason);
                return;
            }
            var itemEntity = res.data;
            $(".itempic").children("img").attr("src",itemEntity.picture);
            $(".itemintro").children(".title").find("p").text(itemEntity.title);
            $(".itemintro").find(".pricenumber").children("span").text(itemEntity.price);
            $(".itemintro").find(".detailText").text(itemEntity.detail);
        }
    });
    //从id获取卖家用户名
    $.ajax({
        type: "GET",
        url: "username",
        dataType: "json",
        data: {
            "id": pid
        },
        timeout: 3000,
        async: false,
        success: function (res) {
            if(res.result == "SUCCESS")
                $(".seller").children("p").text("卖家: "+res.data.username);
            else
                $(".seller").children("p").text("卖家信息获取失败");
        }
    });
    //为购买按钮绑定方法
    $(".buy").click(function () {
        alert("正在发送邮件中");
        //$(this).attr("disabled",true);不能这么写，因为这不是一个按钮
        $(this).unbind("click");
        $(this).click(function () {
            alert("你已经购买，请勿重复购买");
        });
        $.ajax({
            type: "POST",
            url: "index/good/buy",
            dataType: "json",
            data: {
                "sellerid": pid,
                "goodid":gid,
                "goodtitle":$(".itemintro").children(".title").find("p").text()
            },
            timeout: 100000,
            async: true,
            success: function (res) {

                if(res.result == "SUCCESS")
                    alert("恭喜，您已经购买成功，请查看邮箱邮件");
                else
                    alert(res.reason);
            }
        });
    });
});